<template>
  <CompOperate
      :field="field"
      :designer="designer"
      :design-state="designState"
      :parent-list="parentList"
      :parent-widget="parentWidget"
      :sub-form-row-id="subFormRowId"
      :sub-form-row-index="subFormRowIndex"
      :sub-form-col-index="subFormColIndex"
      :index-of-parent-list="indexOfParentList"
      :display-style="field['options'].displayStyle"
  >
    <div class="scene-window-widget" :style="getStyle('area')">
      <div class="window-area">
        <div class="replenish" v-if="optionsInfo['replenishShown']">
          <ScrollX>{{ optionsInfo.replenishContent }}</ScrollX>
        </div>
        <div class="window-info flex-box">
          <div class="flex-box-left">
            <div class="call-patient"><ScrollX>{{ getCurPatientInfo }}</ScrollX></div>
            <div class="wait-patient">
              <div class="title">已签到请等待</div>
              <div class="patient-list" :key="`patient_${widgetKey}`" :data-key="`patient_${widgetKey}`">
                <vue-seamless-scroll
                    class="scroll"
                    :hoverStop="false"
                    :data="getWaitPatientInfo"
                    @ScrollEnd="handleScrollEnd"
                    :ref="`scroll-${widgetKey}`"
                    :class-option="scrollOptions(queueOriginalInfo('wait'),'wait')"
                >
                  <div class="inside-content">
                    <template v-if="!!(getWaitPatientInfo?.length)">
                      <template v-for="(patientItem) in getWaitPatientInfo">
                        <div class="wait-patient-inside" :style="getStyle('waitPatient')">
                          <template v-for="(childItem) in  getPatientChildList(patientItem,3)">
                            <div
                                class="child-item"
                                v-html="getPatientContent(childItem)"
                            />
                          </template>
                        </div>
                      </template>
                    </template>
                    <template v-else>
                      <div class="wait-patient-inside-empty"/>
                    </template>
                  </div>
                </vue-seamless-scroll>
              </div>
            </div>
          </div>
          <div class="flex-box-right">
            <div class="title">请取药</div>
            <div class="sub-title">配药完成未取药</div>
            <div class="patient-list" :key="`patient_${widgetKey}`" :data-key="`patient_${widgetKey}`">
              <vue-seamless-scroll
                  class="scroll"
                  :hoverStop="false"
                  :data="getPhPatientInfo"
                  @ScrollEnd="handleScrollEnd"
                  :ref="`scroll-${widgetKey}`"
                  :class-option="scrollOptions(queueOriginalInfo('phPatient'),'phPatient')"
              >
                <div class="inside-content">
                  <template v-if="!!(getPhPatientInfo?.length)">
                    <template v-for="(patientItem) in getPhPatientInfo">
                      <div class="wait-patient-inside" :style="getStyle('phPatient')">
                        <template v-for="(childItem) in  getPatientChildList(patientItem,2)">
                          <div
                              class="child-item"
                              v-html="getPatientContent(childItem)"
                          />
                        </template>
                      </div>
                    </template>
                  </template>
                  <template v-else>
                    <div class="wait-patient-inside-empty"/>
                  </template>
                </div>
              </vue-seamless-scroll>
            </div>
          </div>
        </div>
      </div>
    </div>
  </CompOperate>
</template>

<script>
import {v4 as uuid} from "uuid";
import {pxToVw} from '@/utils/common';
import emitter from "@/utils/designer/emitter";
import vueSeamlessScroll from 'vue-seamless-scroll';
import {buildParentApi} from "@/utils/designer/util";
import ScrollX from '@c/public/seamlessScroll/ScrollX.vue';
import mixin from "@sc/templateDetails/modules/homeMiddle/fieldMixin";
import CompOperate from "@c/public/form-designer/devType/modules/compOperate.vue";

export default {
  name: "scene-window-patientList4-widget",

  componentName: "FieldWidget", //必须固定为FieldWidget，用于接收父级组件的broadcast事件

  mixins: [emitter, mixin],

  components: {
    CompOperate,
    ScrollX,
    vueSeamlessScroll
  },

  props: {
    widget: Object,
    devType: {
      type: Boolean,
      default: false,
    },
    field: Object,
    parentWidget: Object,
    parentList: Array,
    indexOfParentList: Number,
    designer: Object,
    designState: {
      type: Boolean,
      default: false,
    },
    subFormRowIndex: {
      /* 子表单组件行索引，从0开始计数 */ type: Number,
      default: -1,
    },
    subFormColIndex: {
      /* 子表单组件列索引，从0开始计数 */ type: Number,
      default: -1,
    },
    subFormRowId: {
      /* 子表单组件行Id，唯一id且不可变 */ type: String,
      default: "",
    },
    headTitle: {
      type: String,
      default: "",
    },
    visibleTitle: {
      type: Boolean,
    },
    //prop传入的表单JSON配置
    parentApi: {
      type: Object,
      default: () => buildParentApi()
    },
  },

  data() {
    return {
      widgetKey: uuid(),
      optionsInfo: Object(),
      header: require("@a/images/design/header.jpeg"),
      pause: require("@a/images/design/pause.png"),
    }
  },

  computed: {
    // 就诊患者滚动
    scrollOptions() {
      return function (list, type) {
        let scrollInfo = type === 'wait' ? (this.optionsInfo.waitPatientInfo) : (this.optionsInfo.curPatientInfo);
        let {scroll = false, scrollStep = 1, patientListRow, patientListCol} = scrollInfo;
        let totalNumber = (patientListRow * patientListCol) || 15;
        let fontSize = parseFloat(document.documentElement.style.fontSize);
        let scale = (fontSize / 19.2);
        return {
          hoverStop: true,
          openTouch: false,
          step: (fontSize / 19.2) * scrollStep * scale, // 数值越大速度滚动越快
          direction: (scroll && (list?.length > totalNumber)) ? 1 : -1, // 0向下 1向上 2向左 3向右
          limitMoveNum: (scroll && (list?.length > totalNumber)) ? patientListRow : 99999 // 开始无缝滚动的数据量 this.dataList.length
        }
      }
    },

    // 设置的行内样式
    getStyle() {
      return function (val) {
        const self = this;
        let info = (self.optionsInfo) || Array();
        let Obj = Object();
        let winH = info.replenishArea ? (info.areaHeight - 70) : (info.areaHeight)
        switch (val) {
          case 'area':
            Reflect.set(Obj, 'height', pxToVw(info.areaHeight));
            return Obj;
          case 'waitPatient':
            let waitPatientHeight = winH - 190 - 92 - 4;
            let viewNumber = (info.waitPatientInfo.patientListCol) || 5;
            Reflect.set(Obj, 'height', pxToVw(waitPatientHeight / viewNumber));
            Reflect.set(Obj, 'fontWeight', (info?.waitPatientInfo?.fontWeight));
            Reflect.set(Obj, 'fontSize', pxToVw(info?.waitPatientInfo?.fontSize));
            Reflect.set(Obj, 'color', (info?.waitPatientInfo?.fontColor));
            return Obj;
          case 'phPatient':
            let curPatientHeight = winH - 140 - 78 - 4;
            let viewNumber2 = (info.curPatientInfo.patientListCol) || 5;
            Reflect.set(Obj, 'height', pxToVw(curPatientHeight / viewNumber2));
            Reflect.set(Obj, 'fontWeight', (info?.curPatientInfo?.fontWeight));
            Reflect.set(Obj, 'fontSize', pxToVw(info?.curPatientInfo?.fontSize));
            Reflect.set(Obj, 'color', (info?.curPatientInfo?.fontColor));
            return Obj;

        }
      }
    },

    // 获取科室信息
    getWindowDeptInfo() {
      let devType = (this.devType);
      let roomDoctorInfo = this.$store.getters['getWindowDeptInfoInfo'];
      return devType ? roomDoctorInfo.apiData : roomDoctorInfo.mockData;
    },

    //获取医生-正在就诊患者
    getCurPatientInfo() {
      let devType = (this.devType);
      let roomDoctorInfo = this.$store.getters['getRoomDoctorInfo'];
      let data = devType ? roomDoctorInfo.apiData : roomDoctorInfo.mockData;
      let waitPatient = this.$lodash.map(data, "treatingPatient");
      let flattenArr = this.$lodash.flattenDeep([...waitPatient]) || Array();
      let compactArr = this.$lodash.compact(flattenArr);
      let patientNameInfo = this.$lodash.map(compactArr,'patientName') || Array();
      return this.$lodash.join(patientNameInfo, ',');
    },

    //获取医生-候诊患者
    getWaitPatientInfo() {
      let devType = (this.devType);
      let {patientListRow, patientListCol} = (this.optionsInfo.waitPatientInfo);
      let totalNumber = (patientListRow * patientListCol) || 15;
      let roomDoctorInfo = this.$store.getters['getRoomDoctorInfo'];
      let data = devType ? roomDoctorInfo.apiData : roomDoctorInfo.mockData;
      let waitPatient = this.$lodash.map(data, "waitingPatientList");
      let flattenArr = this.$lodash.compact(this.$lodash.flattenDeep([...waitPatient])) || Array();
      // 不足-补足指定的条数
      if (!!flattenArr && (flattenArr.length) > totalNumber) {
        return this.$lodash.chunk(flattenArr, patientListRow || 3)
      } else {
        let sliceArr = [...flattenArr];
        return this.$lodash.chunk(sliceArr, patientListRow || 3)
      }
    },

    //获取医生-未取药患者
    getPhPatientInfo() {
      let devType = (this.devType);
      let {patientListRow, patientListCol} = (this.optionsInfo.curPatientInfo);
      let totalNumber = (patientListRow * patientListCol) || 10;
      let roomDoctorInfo = this.$store.getters['getRoomDoctorInfo'];
      let data = devType ? roomDoctorInfo.apiData : roomDoctorInfo.mockData;
      let waitingPatient = this.$lodash.map(data, "phPatientList");
      let flattenArr = this.$lodash.compact(this.$lodash.flattenDeep([...waitingPatient])) || Array();
      // 不足-补足指定的条数
      if (!!flattenArr && (flattenArr.length) > totalNumber) {
        return this.$lodash.chunk(flattenArr, patientListRow || 2)
      } else {
        let arr = (new Array(totalNumber - flattenArr.length).fill({}));
        let sliceArr = [...flattenArr, ...arr];
        return this.$lodash.chunk(sliceArr, patientListRow || 2)
      }
    },

    // 子数组填充数据
    getPatientChildList() {
      return function (row, number) {
        let len = row?.length;
        if (len >= number) {
          return row
        } else {
          let fillArr = Array(number - len).fill({});
          return [...row, ...fillArr]
        }
      }
    },

    // 患者内容
    getPatientContent() {
      return function (row) {
        let {patientOrderNoShown = false} = this.optionsInfo;
        if (patientOrderNoShown) {
          return `<span>${row.windowNum || '\u00a0'}</span>&nbsp;${row.patientName || '\u00a0'}`
        } else {
          return `${row.patientName || '\u00a0'}`
        }
      }
    },

    // 原始数据患者拍平
    queueOriginalInfo() {
      return function (type) {
        let devType = (this.devType);
        let queueInfo = this.$store.getters['getRoomDoctorInfo'];
        let data = devType ? queueInfo.apiData : queueInfo.mockData;
        let types = type === 'wait' ? 'waitingPatientList' : 'phPatientList';
        let listInfo = this.$lodash.map(data, types) || Array();
        let flattenArr = this.$lodash.flattenDeep([...listInfo]) || Array();
        return this.$lodash.compact(flattenArr) || Array()
      }
    },
  },

  created() {
    this.registerToRefList();
    this.initEventHandler();
    this.handleOnCreated();
  },

  mounted() {
    this.handleOnMounted();
  },

  methods: {
    // 接收父组件传递过来的数据
    executeMethod(key, data) {
      switch (key) {
        case "roomDoctorInfo":
          console.log(`医生-患者-信息-${data}`);
          this.$store.dispatch('setRoomDoctorInfo', this.$lodash.cloneDeep(data))
          break
        case "roomDeptInfo":
          console.log(`科室诊室信息-${data}`)
          this.$store.dispatch('setWindowDeptInfo', this.$lodash.cloneDeep(data))
          break
      }
    },

    handleScrollEnd() {
    },
  },

  watch: {
    field: {
      handler: function (val) {
        this.widgetKey = uuid();
        let key = val.options.config
        this.optionsInfo = val.options[key];
      },
      deep: true,
      immediate: true
    }
  },

  destroyed() {
    this.unregisterFromRefList();
  },

};
</script>

<style lang="scss" scoped>
.scene-window-widget {
  color: #333;
  height: 150px;
  font-size: 40px;
  padding: 28px 30px;
  box-sizing: border-box;
  flex: inherit;
  flex-direction: column;
  justify-content: space-between;
  background: #E5EAEF;;
  overflow: hidden;

  .window-area {
    height: 100%;
    display: flex;
    flex-direction: column;
    background: transparent;

    .replenish {
      width: 100%;
      font-size: 80px;
      color: #0059B3;
      line-height: 112px;
      text-align: center;
      font-style: normal;
      @include fontWeight()
    }

    .window-info {
      .flex-box-left {
        flex-basis: calc(60% - 20px);
        background: rgba(0, 89, 179, 0.9);
        border-radius: 20px;
        overflow: hidden;
        box-sizing: border-box;
        margin-right: 20px;
        box-shadow: 30px 30px 30px -10px rgba(0, 0, 0, 0.15),
        inset -1px -1px 10px rgba(0, 0, 0, 0.2);
        display: flex;
        flex-direction: column;
        font-weight: normal;

        .call-patient {
          display: flex;
          min-height: 190px;
          font-size: 120px;
          width: 100%;
          padding: 0 20px;
          box-sizing: border-box;
          color: rgba(255, 255, 255, 1);
          align-self: center;
          font-weight: bold;
          overflow: hidden;
          align-items: center;
        }

        .wait-patient {
          width: 100%;
          overflow: hidden;
          min-height: calc(100% - 190px);

          .title {
            display: flex;
            align-self: center;
            align-items: center;
            justify-content: center;
            align-content: center;
            text-align: center;
            height: 108px;
            width: 100%;
            font-size: 62px;
            overflow: hidden;
            box-sizing: border-box;
            color: rgba(255, 255, 255, 1);
            background: rgba(0, 79, 160, 1);
            @include singleEllipsis(100%);
          }

          .patient-list {
            display: flex;
            flex-wrap: wrap;
            width: 100%;
            overflow: hidden;
            height: calc(100% - 128px);

            .scroll {
              width: 100%;
              /* 确保内容样式不会导致不必要的布局变化 */
              white-space: nowrap;
              font-smoothing: antialiased; /* 抗锯齿，使字体更清晰 */
            }

            .inside-content {
              display: flex;
              flex-direction: column;
              width: 100%;
              align-items: center;
              align-self: center;
              align-content: center;
              justify-content: center;
              /* 确保内容样式不会导致不必要的布局变化 */
              white-space: nowrap;
              font-smoothing: antialiased; /* 抗锯齿，使字体更清晰 */

              .wait-patient-inside {
                box-sizing: border-box;
                font-size: 42px;
                width: 100%;
                display: flex;
                flex-direction: row;
                overflow: hidden;
                color: #E8E8E8;
                text-align: center;
                border-bottom: 1px rgba(51, 51, 51, 0.2) solid;
                /* 确保内容样式不会导致不必要的布局变化 */
                white-space: nowrap;
                font-smoothing: antialiased; /* 抗锯齿，使字体更清晰 */

                .child-item {
                  flex-grow: 1;
                  flex-shrink: 0;
                  flex-basis: 0;
                  align-content: center;
                  align-self: center;
                  align-items: center;
                  @include singleEllipsis(100%);
                  /* 确保每个滚动项的样式不会导致布局抖动 */
                  display: inline-block;
                  vertical-align: top; /* 确保垂直对齐一致 */
                  font-smoothing: antialiased; /* 抗锯齿，使字体更清晰 */
                }
              }
            }

            .wait-patient-inside-empty {
              min-height: 480px;
              margin-top: 10px;
              background: white;
            }
          }
        }
      }

      .flex-box-right {
        flex-basis: calc(40%);
        display: flex;
        flex-direction: column;
        border-radius: 20px;
        box-sizing: border-box;
        overflow: hidden;
        box-shadow: 30px 30px 30px -10px rgba(0, 0, 0, 0.15),
        inset 15px 15px 10px rgba(255, 255, 255, 0.75),
        inset -1px -1px 10px rgba(0, 0, 0, 0.2);

        .title {
          font-size: 82px;
          color: white;
          display: flex;
          align-items: center;
          align-self: center;
          justify-content: center;
          text-align: center;
          box-sizing: border-box;
          height: 140px;
          width: 100%;
          background: #0059b3;
        }

        .sub-title {
          display: flex;
          align-items: center;
          align-self: center;
          justify-content: center;
          text-align: center;
          box-sizing: border-box;
          width: 100%;
          height: 98px;
          font-size: 56px;
          color: #0059b3;
          background: rgba(232, 232, 232, 0.4);
        }

        .patient-list {
          width: 100%;
          display: flex;
          flex-direction: column;
          overflow: hidden;
          height: calc(100% - 140px - 98px);

          .scroll {
            width: 100%;
            /* 确保内容样式不会导致不必要的布局变化 */
            white-space: nowrap;
            font-smoothing: antialiased; /* 抗锯齿，使字体更清晰 */
          }

          .inside-content {
            display: flex;
            flex-direction: column;
            width: 100%;
            align-items: center;
            align-self: center;
            align-content: center;
            justify-content: center;
            /* 确保内容样式不会导致不必要的布局变化 */
            white-space: nowrap;
            font-smoothing: antialiased; /* 抗锯齿，使字体更清晰 */

            .wait-patient-inside {
              color: #333333;
              box-sizing: border-box;
              font-size: 32px;
              width: 100%;
              display: flex;
              flex-direction: row;
              overflow: hidden;
              background: rgba(255, 255, 255, 1);
              text-align: center;
              border-bottom: 1px rgba(51, 51, 51, 0.2) solid;
              /* 确保内容样式不会导致不必要的布局变化 */
              white-space: nowrap;
              font-smoothing: antialiased; /* 抗锯齿，使字体更清晰 */

              .child-item {
                flex-grow: 1;
                flex-shrink: 0;
                flex-basis: 0;
                align-content: center;
                align-self: center;
                align-items: center;
                @include singleEllipsis(100%);
                /* 确保每个滚动项的样式不会导致布局抖动 */
                display: inline-block;
                vertical-align: top; /* 确保垂直对齐一致 */
                font-smoothing: antialiased; /* 抗锯齿，使字体更清晰 */
              }
            }
          }

          .wait-patient-inside-empty {
            min-height: 480px;
            margin-top: 10px;
            background: white;
          }
        }
      }
    }
  }
}
</style>
